package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理Controller
 */
@RestController
@Slf4j
public class DeptController {

//    或者
//    private static Logger log= LoggerFactory.getLogger(DeptController.class);

    @Autowired
   private DeptService deptService;



    //    @RequestMapping("/depts")
    @GetMapping("/depts")
    public Result list(){

        List<Dept> dept_list=deptService.list();
        log.info("查询全部的部门数据");
        return Result.success(dept_list);
    }


    @DeleteMapping ("/depts/{id}")
    public  Result delDept(@PathVariable  int id){
        deptService.delDept(id);
        log.info("部门删除操作");
        return Result.success(null);
    }

    @PostMapping("/depts")
    public Result  addDept(@RequestBody Dept dept){
        deptService.addDept(dept);
        log.info("部门新增操作"+dept.toString());  //部门新增操作Dept(id=null, name=学习部1, createTime=null, updateTime=null)
        return Result.success(null);
    }

    @PutMapping ("/depts")
    public  Result updateDept(@RequestBody Dept dept){

        deptService.updateDept(dept);
        return Result.success(null);
    }

//    @GetMapping(value = "depts/{id}",params = {"id"})
    @GetMapping(value = "depts/{id}")
    public Result queryById(@PathVariable Integer id){

        System.out.println( id);


        Dept dept1= deptService.queryById(id);

        return Result.success(dept1);

    }
}
